class Restaurants::ChartsController < Restaurants::RestaurantsController
  
  def sellers
    @begin_date = params[:begin_date] || 8.day.ago
    @end_date = params[:end_date] || 1.day.ago
    # @orders = Order.where(status: Order::FINISH).where('date(created_at) >= ? and date(created_at) <= ?',@begin_date,@end_date)
    @food_charts = Food.find_by_sql [
      "
        select foods.name,ifnull(sum(item.quantity),0) as total_quantity,ifnull(sum(item.total_price),0) as total_sell_price FROM
        foods left join 
          (
            select order_items.food_id,order_items.quantity,order_items.single_price*order_items.quantity as total_price from order_items 
            where order_items.order_id in 
            (
              select id from orders where orders.status = 3 and date(orders.created_at) >= :begin_date and date(orders.created_at) <= :end_date
            )
          ) item on foods.id = item.food_id group by foods.id order by total_quantity desc
      ",{begin_date: @begin_date,end_date:@end_date}
    ]
  end 

  def orders
    @order_charts = Order.select("date(created_at) as sell_date, count(*) as day_count").where(status: Order::FINISH).where("date(created_at) >= ? ",30.days.ago).group("date(created_at)")
  end 


end
